<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Models\Notice;
use App\Models\Auth;
use App\Models\NoticeManager;

class NoticeController extends Controller
{
    public function index(Request $request, Notice $notice)
    {
    	$notices = $notice->orderBy('id', 'desc')->paginate(config('api.page.limit'));
    	return view('admins.notice.index', compact('notices'));
    }

    public function create()
    {
    	return view('admins.notice.add');
    }

    public function store(Request $request, Notice $notice)
    {
    	$result = $notice->create($request->all());
    	if(!$result){
            return back()->with('warning','添加失败');
        }
        return redirect()->route('admin.notices.index')->with('success', '添加成功');
    }

    public function edit(Notice $notice)
    {
    	return view('admins.notice.edit', compact('notice'));
    }


    public function update(Request $request, Notice $notice)
    {
    	$result = $notice->update($request->all());
    	if(!$result){
            return back()->with('warning','编辑失败');
        }
        return redirect()->route('admin.notices.index')->with('success', '编辑成功');
    }


    public function delete(Request $request, Notice $notice)
    {
    	$notice = $notice->find($request->id);
        $notice->delete();
        return json_encode(['code' => 200, 'message' => '删除成功']);
    }

    public function publishNotice(Request $request, Notice $notice, Auth $auth)
    {
    	$notice = $notice->find($request->id);
    	$type = $notice->type;
        if($type != 0){
            if($type == 5){
                $auth = $auth->where('type', '>', 1);
            }else{
                $auth = $auth->where('type', $type);
            }
        }    	
        if($notice->recipient){
            $recipient = explode(',', $notice->recipient);
            $auth = $auth->whereIn('id',$recipient);
        }
    	$managers = $auth->orderBy('id', 'desc')->get();
    	$notice->notice_manager()->delete();
    	foreach ($managers as $key => $value) {
    		$data[] = new NoticeManager([
    			'notice_id' => $notice->id,
    			'manager_id' => $value->id,
    			'read_state' => 0,
                'type' => 1,
    		]);
    	}
    	$res = $notice->notice_manager()->saveMany($data);
    	if($res){
    		return json_encode(['code' => 200, 'message' => '发布成功']);
    	}else{
    		return json_encode(['code' => 400, 'message' => '发布成功']);
    	}
    }

    public function noticeTotal(NoticeManager $notice_manager)
    {
    	return json_encode(['code' => 200, 'msg' => 'success', 'data' => $notice_manager->where('manager_id',auth('admin')->user()->id)->where('read_state',0)->count()]);
    }

    public function lists(NoticeManager $notice_manager)
    {
    	$notice_manager->where('manager_id', auth('admin')->user()->id)->update(['read_state' => 1]);
    	$notice_lists = $notice_manager->with('notice')->has('notice')->where('manager_id', auth('admin')->user()->id)->orderBy('id','desc')->get();
    	return json_encode(['code' => 200, 'msg' => 'success', 'data' => $notice_lists]);
    }
}
